#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void sig_hander(int sig)
{
    if (sig == SIGINT || sig == SIGTERM)
    {
        printf("Caught signal %d\n", sig);
        exit(EXIT_FAILURE);
    }
}
int main(void)
{
    // 注册信号(SIGINT/SIGTERM)处理函数
    if (signal(SIGINT, sig_hander) == SIG_ERR)
    {
        printf("注册SIGINT信号失败\n");
        return -1;
    }
    if (signal(SIGTERM, sig_hander) == SIG_ERR)
    {
        printf("注册SIGTERM信号失败\n");
        return -1;
    }

    while (1)
    {
        printf("程序正在运行\n");
        sleep(1);
    }
}